/*
 * AppsListener.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.core.hotdeploy;

import java.io.File;

import org.eu4ria.core.ArchiveDeployer;

public class AppsListener extends BaseListener implements IFileListener {



    public AppsListener() {
        super();
    }

    public void onStart(Object monitoredResource) {

        if (monitoredResource instanceof File) {
            File resource = (File) monitoredResource;
            if (resource.isDirectory()) {

                System.out.println("Start to monitor " + resource.getAbsolutePath());

            }
        }
    }

    public void onStop(Object notMonitoredResource) {

    }

    public void onAdd(Object newResource) {
        if (newResource instanceof String) {
            File file = new File((String)newResource);
            if (file.isFile()&&file.getAbsolutePath().endsWith(".jar")) {
                ArchiveDeployer deployer = ArchiveDeployer.getInstance();
                if(deployer.deployApplication(file, true)){
                	System.out.print(file.getName() + " found,deploying application...DONE");
                }else{
                	System.out.print(file.getName() + " found,deploying application...FAILED");
                }
                System.out.println();
            }
        }
    }

    public void onChange(Object changedResource) {
        if (changedResource instanceof String) {
        	File file = new File((String)changedResource);
            if (file.isFile()&&file.getAbsolutePath().endsWith(".jar")) {
                ArchiveDeployer deployer = ArchiveDeployer.getInstance();
            	deployer.undeployApplication(file.getName());
                if(deployer.deployApplication(file, true)){
                	System.out.print("Redeploying "+ file.getName() + " ...DONE");
                }else{
                	System.out.print("Redeploying "+ file.getName() + " ...FAILED");
                }
                System.out.println();
            }

        }
    }

    public void onDelete(Object deletedResource) {
        if (deletedResource instanceof File) {
            String deletedFile = ((File) deletedResource).getName();

            ArchiveDeployer deployer = ArchiveDeployer.getInstance();
            if(deployer.undeployApplication(deletedFile)){
            	System.out.print("Undeploying "+  deletedFile +"...DONE");
            }else{
            	System.out.print("Undeploying "+ deletedFile +"...FAILED");
            }
            System.out.println();
        }
    }
}

